import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart' ;
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:mfkb/config/Config.dart';

class SwiperHeadWidget extends StatefulWidget {
  dynamic data = null ;
  double h = null ;

  SwiperHeadWidget({Key key , @required this.data , this.h}):super(key:key) ;

  @override
  _SwiperHeadWidgetState createState() => _SwiperHeadWidgetState();
}

class _SwiperHeadWidgetState extends State<SwiperHeadWidget> {

  int _swiper_index = 0 ;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          height:widget.h == null ? ScreenUtil().setWidth(375) * (280 / 375) : ScreenUtil().setWidth(widget.h) ,
          child:Swiper(
            itemCount: widget.data == null || widget.data["banners"].length == 0 ? 1 : widget.data["banners"].length ,
            loop: true,
            autoplay: true,
            itemBuilder: (context , index){
              return Container(
                child: CachedNetworkImage(
                  imageUrl: widget.data == null || widget.data["banners"].length == 0 ? "" : Config().getImgHost()+widget.data["banners"][index]["url"] ,
                  fit: BoxFit.fill ,
                  placeholder: (contex , url){
                    return Container(
                      color:Color.fromARGB(255, 246, 245, 248) ,
                      child: Center(
                        child: Image.asset("assets/images/imageloading.png"),
                      ) ,
                    ) ;
                  },
                ) ,
              ) ;
            },
            onIndexChanged: (index){
              setState(() {
                _swiper_index = index ;
              });
            },
          ) ,
        ) ,
        Positioned(
          bottom: 0,
          right: 0,
          left: 0,
          child: Container(
            height: ScreenUtil().setHeight(10),
            decoration: BoxDecoration(
              color: Colors.white ,
              borderRadius: BorderRadius.only(topLeft: Radius.circular(12) , topRight: Radius.circular(12))
            ),
          )
        ) ,
        Positioned(
          bottom: ScreenUtil().setHeight(20),
          right:ScreenUtil().setWidth(15) ,
          child: Container(
            width: 46,
            height: 28,
            decoration: BoxDecoration(
              color: Color.fromARGB(180 , 0, 0, 0),
              borderRadius: BorderRadius.all(Radius.circular(15))
            ),
            child: Container(
              width: ScreenUtil().setWidth(26),
              margin: EdgeInsets.only(left:ScreenUtil().setWidth(12)),
              child: Center(
                child: Row(
                  children: <Widget>[
                    Text((_swiper_index+1).toString() , style: TextStyle(
                      fontSize: 14 ,
                      color: Colors.white ,
                    ),) ,
                    Text(widget.data != null && widget.data["banners"].length != 0 ? " /"+widget.data["banners"].length.toString() : " /1" , style: TextStyle(
                      fontSize: 11 ,
                      color: Colors.white ,
                      height: 1.6
                    ),)
                  ],
                ),
              ),
            )
          )
        )
      ],
    );
  }
}